//
//  TransacionViewModel.m
//  Futures
//
//  Created by Cheng on 2017/7/13.
//  Copyright © 2017年 Cheng. All rights reserved.
//

#import "TransacionViewModel.h"
#import "HoldingModel.h"
#import "CloseOrderModel.h"
#import "SymbolModel.h"
@implementation TransacionViewModel

/**
 * 14、获取能交易的所有合约列表
 * http://139.224.8.133:9100/api/get_symbols.ashx
 * 传参：account
 */
+ (void)requestSymbolsWithBlock:(void (^)(id obj))block{
    MYLog(@"GLOBAL_ISLOGIN = %d",GLOBAL_ISLOGIN);
    if (!GLOBAL_ISLOGIN) return;
    
    NSMutableArray *models = [NSMutableArray array];
    NSMutableArray *datas = [NSMutableArray array];
    
    NSDictionary *dict = @{@"account":GLOBAL_INSTANCE.user.cid};
    [Network requestDataMethod:GET WithUrl:URL_SYMBOLS parameters:dict success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *array = responseObject;
        for (NSDictionary *dict in array) {
            SymbolModel *model = [[SymbolModel alloc] initWithDict:dict];
            [models addObject:model];
        }
        NSMutableString *suffix = [NSMutableString stringWithFormat:URL_QUOTE_LIST];
        [suffix appendString:@"?"];
        for (SymbolModel *model in models) {
            NSString *symbol;
            if ([models indexOfObject:model] == 0) {
                symbol = [@"symbol=" stringByAppendingString:model.Symbol];
            }else{
                symbol = [@"&symbol=" stringByAppendingString:model.Symbol];
            }
            NSString *exchange = [@"&exchange=" stringByAppendingString:model.Exchange];
            [suffix appendString:symbol];
            [suffix appendString:exchange];
        }
        
        NSString *urlStr = [NSString stringWithFormat:@"%@",suffix];
//        urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet  URLQueryAllowedCharacterSet]];
        MYLog(@"%@",urlStr);
        [Network requestDataMethod:GET WithUrl:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSArray *arr = responseObject;
            for (NSDictionary *dict in arr) {
                QuoteModel *quoteModel = [[QuoteModel alloc] initWithDict:dict];
                [datas addObject:quoteModel];
            }
            block(datas);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


//12、获取持仓（持仓盈亏，所有持仓）
+ (void)requestHoldingWithBlck:(void (^)(id obj))block{
    NSMutableArray *models = [NSMutableArray array];
    NSDictionary *dict = @{@"account":GLOBAL_INSTANCE.user.cid};
    [Network requestDataMethod:GET WithUrl:URL_HOLDING parameters:dict success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *array = responseObject;
        for (NSDictionary *dict in array) {
            HoldingModel *model = [[HoldingModel alloc] initWithDict:dict];
            [models addObject:model];
        }
        block(models);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

//17、获取平仓单（结算单用）-- 订单记录（订单详情）//account,startdate,enddate
+ (void)requestCloseOderWithBlck:(void (^)(id obj))block{
    NSMutableArray *models = [NSMutableArray array];
    NSDictionary *dict = @{@"account":GLOBAL_INSTANCE.user.cid,
                           @"startdate":@"2010-07-07 12:00:00",
                           @"enddate":[NSString getCurrentFullTime]};
    
    [Network requestDataMethod:GET WithUrl:URL_CLOSED_ORDER parameters:dict success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *array = responseObject;
        for (NSDictionary *dict in array) {
            CloseOrderModel *model = [[CloseOrderModel alloc] initWithDict:dict];
            [models addObject:model];
            MYLog(@"%@",model.properties_aps);
        }
        block(models);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}





@end
